package com.show.mytest;

import java.io.File;
import java.io.FileFilter;

public class Test06_Filter {
    public static void main(String[] args) {
        // 创建file对象
        File file = new File("F:/ThunderDownload");
        /*// 调用list()方法获得String数组
        String[] list = file.list();
        for (String s : list) {
            System.out.println(s);
        }*/
        // 调用方法获得遍历
//        printFiles(file);
//        printFiles1(file);
        printFiles2(file);
    }

    private static void printFiles2(File file) {
        if (file.isFile()){
            System.out.println(file);
            return;
        }
        File[] files = file.listFiles(f ->
          f.isDirectory() || f.getName().endsWith(".exe")
        );
        for (File file1 : files) {
            if (file1.isFile()){
                System.out.println(file1);
            } else {
                printFiles(file1);
            }
        }
    }

    private static void printFiles1(File file) {
        if (file.isFile()){
            System.out.println(file);
            return;
        }
        File[] files = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if (file.isDirectory() || file.getName().endsWith(".exe")) return true;
                return false;
            }
        });
        for (File file1 : files) {
            if (file1.isFile()){
                System.out.println(file1);
            } else {
                printFiles(file1);
            }
        }
    }

    private static void printFiles(File file) {
        if (file.isFile()){
            System.out.println(file);
            return;
        }
        Myfilter filter = new Myfilter();
        File[] files = file.listFiles(filter);
        for (File file1 : files) {
            if (file1.isFile()){
                System.out.println(file1);
            } else {
                printFiles(file1);
            }
        }
    }

    static class Myfilter implements FileFilter{

        @Override
        public boolean accept(File pathname) {
            if (pathname.isDirectory() || pathname.getName().endsWith(".exe")) return true;
            return false;
        }
    }
}
